<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="赞赏是否开启" prop="appreciation">
      <el-input v-model="dataForm.appreciation" placeholder="赞赏是否开启"></el-input>
    </el-form-item>
    <el-form-item label="评论是否开启" prop="commentabled">
      <el-input v-model="dataForm.commentabled" placeholder="评论是否开启"></el-input>
    </el-form-item>
    <el-form-item label="内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="内容"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="描述" prop="description">
      <el-input v-model="dataForm.description" placeholder="描述"></el-input>
    </el-form-item>
    <el-form-item label="首图" prop="firstPicture">
      <el-input v-model="dataForm.firstPicture" placeholder="首图"></el-input>
    </el-form-item>
    <el-form-item label="标记" prop="flag">
      <el-input v-model="dataForm.flag" placeholder="标记"></el-input>
    </el-form-item>
    <el-form-item label="是否发布" prop="published">
      <el-input v-model="dataForm.published" placeholder="是否发布"></el-input>
    </el-form-item>
    <el-form-item label="是否推荐" prop="recommend">
      <el-input v-model="dataForm.recommend" placeholder="是否推荐"></el-input>
    </el-form-item>
    <el-form-item label="转载声明是否开启" prop="shareStatement">
      <el-input v-model="dataForm.shareStatement" placeholder="转载声明是否开启"></el-input>
    </el-form-item>
    <el-form-item label="标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="浏览次数" prop="views">
      <el-input v-model="dataForm.views" placeholder="浏览次数"></el-input>
    </el-form-item>
    <el-form-item label="分类id" prop="typeId">
      <el-input v-model="dataForm.typeId" placeholder="分类id"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="浏览次数" prop="commentCount">
      <el-input v-model="dataForm.commentCount" placeholder="浏览次数"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          blogId: 0,
          appreciation: '',
          commentabled: '',
          content: '',
          createTime: '',
          description: '',
          firstPicture: '',
          flag: '',
          published: '',
          recommend: '',
          shareStatement: '',
          title: '',
          updateTime: '',
          views: '',
          typeId: '',
          userId: '',
          commentCount: ''
        },
        dataRule: {
          appreciation: [
            { required: true, message: '赞赏是否开启不能为空', trigger: 'blur' }
          ],
          commentabled: [
            { required: true, message: '评论是否开启不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '内容不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ],
          firstPicture: [
            { required: true, message: '首图不能为空', trigger: 'blur' }
          ],
          flag: [
            { required: true, message: '标记不能为空', trigger: 'blur' }
          ],
          published: [
            { required: true, message: '是否发布不能为空', trigger: 'blur' }
          ],
          recommend: [
            { required: true, message: '是否推荐不能为空', trigger: 'blur' }
          ],
          shareStatement: [
            { required: true, message: '转载声明是否开启不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          views: [
            { required: true, message: '浏览次数不能为空', trigger: 'blur' }
          ],
          typeId: [
            { required: true, message: '分类id不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          commentCount: [
            { required: true, message: '浏览次数不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.blogId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.blogId) {
            this.$http({
              url: this.$http.adornUrl(`/myblogweb/blog/info/${this.dataForm.blogId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.appreciation = data.blog.appreciation
                this.dataForm.commentabled = data.blog.commentabled
                this.dataForm.content = data.blog.content
                this.dataForm.createTime = data.blog.createTime
                this.dataForm.description = data.blog.description
                this.dataForm.firstPicture = data.blog.firstPicture
                this.dataForm.flag = data.blog.flag
                this.dataForm.published = data.blog.published
                this.dataForm.recommend = data.blog.recommend
                this.dataForm.shareStatement = data.blog.shareStatement
                this.dataForm.title = data.blog.title
                this.dataForm.updateTime = data.blog.updateTime
                this.dataForm.views = data.blog.views
                this.dataForm.typeId = data.blog.typeId
                this.dataForm.userId = data.blog.userId
                this.dataForm.commentCount = data.blog.commentCount
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/myblogweb/blog/${!this.dataForm.blogId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'blogId': this.dataForm.blogId || undefined,
                'appreciation': this.dataForm.appreciation,
                'commentabled': this.dataForm.commentabled,
                'content': this.dataForm.content,
                'createTime': this.dataForm.createTime,
                'description': this.dataForm.description,
                'firstPicture': this.dataForm.firstPicture,
                'flag': this.dataForm.flag,
                'published': this.dataForm.published,
                'recommend': this.dataForm.recommend,
                'shareStatement': this.dataForm.shareStatement,
                'title': this.dataForm.title,
                'updateTime': this.dataForm.updateTime,
                'views': this.dataForm.views,
                'typeId': this.dataForm.typeId,
                'userId': this.dataForm.userId,
                'commentCount': this.dataForm.commentCount
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
